package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.atguigu.jxc.vo.OverflowSaveGoodsVo;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ONIE
 * @create 2021/11/26 10:29 上午
 */
@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {

	@Autowired
	private OverflowListGoodsDao overflowListGoodsDao;
	@Autowired
	private OverflowListDao overflowListDao;

	@Override
	public void saveOverflowSaveGoods(String overflowNumber, OverflowSaveGoodsVo overflowSaveGoodsVo) {
		String overflowListGoodsStr = overflowSaveGoodsVo.getOverflowListGoodsStr();
		Gson gson = new Gson();
		JsonArray jsonElements = gson.fromJson(overflowListGoodsStr, JsonArray.class);
		OverflowList overflowList = new OverflowList();
		overflowList.setOverflowDate(overflowSaveGoodsVo.getOverflowDate());
		overflowList.setRemarks(overflowSaveGoodsVo.getRemarks());
		overflowList.setOverflowNumber(overflowNumber);
		overflowListDao.saveOverflowList(overflowList);

		List<OverflowListGoods> overflowListGoodsList = new ArrayList<>();
		for (JsonElement jsonElement : jsonElements) {
			OverflowListGoods overflowListGoods = gson.fromJson(jsonElement, OverflowListGoods.class);
			overflowListGoods.setOverflowListId(overflowList.getOverflowListId());
			overflowListGoodsList.add(overflowListGoods);
		}
		overflowListGoodsDao.saveOverflowListGoodsList(overflowListGoodsList);
	}

	@Override
	public List<OverflowList> listOverflow(String sTime, String eTime) {
		return overflowListDao.listOverflow(sTime,eTime);
	}

	@Override
	public List<OverflowListGoods> goodsList(Integer overflowListId) {
		return overflowListGoodsDao.goodsList(overflowListId);
	}
}
